package icu.qx.pro.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.api.domain.SysUser;
import icu.qx.common.constant.TableNameEnum;
import icu.qx.common.utils.CodeUtils;
import icu.qx.pro.domain.ProWorkOrder;
import icu.qx.pro.domain.ProWorkPeo;
import icu.qx.pro.domain.ProWorkReport;
import icu.qx.pro.domain.param.ProWorkTaskParam;
import icu.qx.pro.domain.pbs.PbsProcedure;
import icu.qx.pro.domain.pbs.PbsProcedureRoute;
import icu.qx.pro.domain.pbs.PbsWorkCell;
import icu.qx.pro.feign.SysUserFeign;
import icu.qx.pro.service.IProWorkOrderService;
import icu.qx.pro.service.IProWorkPeoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.pro.domain.ProWorkTask;
import icu.qx.pro.service.IProWorkTaskService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 工单任务Controller
 *
 * @author yong
 * @date 2024-06-24
 */
@RestController
@RequestMapping("/pro/workTask")
public class ProWorkTaskController extends BaseController {
    @Autowired
    private IProWorkTaskService proWorkTaskService;

    @Autowired
    private IProWorkOrderService proWorkOrderService;

    @Autowired
    private IProWorkPeoService proWorkPeoService;

    @Autowired
    private SysUserFeign sysUserFeign;

    @Autowired
    private CodeUtils codeUtils;

    /**
     * 查询工单任务列表
     */
    @RequiresPermissions("pro:workTask:list")
    @GetMapping("/list")
    public TableDataInfo list(ProWorkTask proWorkTask) {
        startPage();
        List<ProWorkTask> list = proWorkTaskService.selectProWorkTaskList(proWorkTask);
        return getDataTable(list);
    }

    /**
     * 查询工单任务条件的内容
     */
    @RequiresPermissions("pro:workTask:list")
    @GetMapping("/condition")
    public AjaxResult condition() {
        AjaxResult ajaxResult = success();
        List<PbsProcedureRoute> pbsProcedureRoutes = proWorkTaskService.selectRouteInTask();
        List<PbsProcedure> pbsProcedures = proWorkTaskService.selectProcedureInTask();
        List<PbsWorkCell> pbsWorkCells = proWorkTaskService.selectWorkCellInTask();
        ajaxResult.put("pbsProcedureRoutes", pbsProcedureRoutes);
        ajaxResult.put("pbsProcedures", pbsProcedures);
        ajaxResult.put("pbsWorkCells", pbsWorkCells);
        return ajaxResult;
    }

    /**
     * 导出工单任务列表
     */
    @RequiresPermissions("pro:workTask:export")
    @Log(title = "工单任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProWorkTask proWorkTask) {
        List<ProWorkTask> list = proWorkTaskService.selectProWorkTaskList(proWorkTask);
        ExcelUtil<ProWorkTask> util = new ExcelUtil<ProWorkTask>(ProWorkTask.class);
        util.exportExcel(response, list, "工单任务数据");
    }

    /**
     * 获取工单任务详细信息
     */
    @RequiresPermissions("pro:workTask:query")
    @GetMapping(value = "/{wtaskCode}")
    public AjaxResult getInfo(@PathVariable("wtaskCode") String wtaskCode) {
        AjaxResult ajaxResult = success();
        ProWorkTask proWorkTask = proWorkTaskService.selectProWorkTaskByWtaskCode(wtaskCode);
        ProWorkOrder proWorkOrder = proWorkOrderService.selectProWorkOrderByWorkOrderCode(proWorkTask.getWtaskWoCode());
        ProWorkPeo proWorkPeo = new ProWorkPeo();
        proWorkPeo.setWpWtaskCode(wtaskCode);
        proWorkPeo.setWpWgCode(proWorkTask.getWtaskGroup());
        List<ProWorkPeo> proWorkPeos = proWorkPeoService.selectProWorkPeoList(proWorkPeo);
        proWorkTask.setWtaskManagerName(sysUserFeign.username(proWorkTask.getWtaskManager()).getData().getUserName());
        proWorkTask.setWtaskQcPeoName(sysUserFeign.username(proWorkTask.getWtaskQcPeo()).getData().getUserName());
        ajaxResult.put("data", proWorkTask);
        ajaxResult.put("proWorkOrder", proWorkOrder);
        ajaxResult.put("proWorkPeos", proWorkPeos);
        return ajaxResult;
    }

    /**
     * 获取工单任务生产人
     */
    @RequiresPermissions("pro:workTask:query")
    @GetMapping(value = "/user/{wtaskCode}")
    public AjaxResult getUser(@PathVariable("wtaskCode") String wtaskCode) {
        List<SysUser> sysUsers = proWorkTaskService.selectTaskPeo(wtaskCode);
        return success(sysUsers);
    }

    /**
     * 新增工单任务
     */
    @RequiresPermissions("pro:workTask:add")
    @Log(title = "工单任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProWorkTask proWorkTask) {
        return toAjax(proWorkTaskService.insertProWorkTask(proWorkTask));
    }

    /**
     * 派工
     */
    @RequiresPermissions("pro:workTask:edit")
    @Log(title = "工单任务", businessType = BusinessType.UPDATE)
    @PutMapping("/arrangePeo")
    public AjaxResult arrangePeo(@RequestBody ProWorkTaskParam proWorkTaskParam) {
        return success(proWorkTaskService.arrangePeo(proWorkTaskParam.getProWorkTask(), proWorkTaskParam.getProWorkPeo()));
    }

    /**
     * 开工
     */
    @RequiresPermissions("pro:workTask:edit")
    @Log(title = "工单任务", businessType = BusinessType.UPDATE)
    @PutMapping("/startWork")
    public AjaxResult startWork(@RequestBody ProWorkTaskParam proWorkTaskParam) {
        return toAjax(proWorkTaskService.startWork(proWorkTaskParam.getProWorkTask()));
    }

    /**
     * 删除工单任务
     */
    @RequiresPermissions("pro:workTask:remove")
    @Log(title = "工单任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wtaskCodes}")
    public AjaxResult remove(@PathVariable String[] wtaskCodes) {
        return toAjax(proWorkTaskService.deleteProWorkTaskByWtaskCodes(wtaskCodes));
    }
}
